package com.threadmethod.volatiled;

import com.createthread.p1.MyThread;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * @PackageName:com.threadmethod.volatiled
 * @ClassName:Test03
 * @Description: 使用原子类进行自增
 * @Author: zhangxiaoyu
 * @date 2020/12/10 16:05
 */
public class Test04 {

    public static void main(String[] args) throws InterruptedException {
         //创建10个现线程
        for (int i=0;i<100;i++){
            new MyThread().start();
        }
        
        Thread.sleep(1000);
        System.out.println(MyThread.count.get());
    }


    static  class  MyThread extends  Thread{

        //使用AtomicInteger对象
        private static AtomicInteger count = new AtomicInteger();


        public static void addCount(){
            for (int i = 0;i<1000;i++){
                //自增的后缀形式
                count.getAndIncrement();
            }
            System.out.println(Thread.currentThread().getName()+"count="+count.get());

        }

        @Override
        public void run() {
            addCount();
        }
    }
}
